﻿using System.IO;
using EnvDTE;
using Microsoft.Practices.RecipeFramework;
using VSSDKAssist.Assets.Actions.Base;
using Microsoft.Practices.RecipeFramework.Library;

namespace VSSDKAssist.Recipes.CustomProject.CreateCustomProject
{
    public class UnfoldCustomProjectTemplateAction : ActionBase
    {
        [Input(Required = true)]
        public string ProjectName { get; set; }

        [Output]
        public Project Project { get; set; }

        protected override void OnExecute()
        {
            if(DteHelper.FindProjectByName(VisualStudio, this.ProjectName) == null)
            {
                string projectFileName = string.Concat(Path.GetDirectoryName(VisualStudio.Solution.FileName), @"\", this.ProjectName);

                VisualStudio.Solution.AddFromTemplate(
                    Path.Combine(this.PackageBasePath, @"Templates\ExcludedProjects\CustomProject\ProjectBase.vstemplate"),
                    projectFileName,
                    this.ProjectName,
                    false);
                
                this.Project = DteHelper.FindProjectByName(VisualStudio, this.ProjectName);

                //This is because of a bug on the unfolding process
                if(this.Project != null)
                {
                    //this.Project.Save(this.ProjectName);
                    //VisualStudio.ExecuteCommand("Project.UnloadProject", string.Empty);
                    //VisualStudio.ExecuteCommand("Project.ReloadProject", string.Empty);
                }
            }
        }

        protected override void OnUndo()
        {
            //Do Nothing
        }
    }
}